const {
    getDepartmentBySearch,
    getMajorBySearch,
    getClassBySearch,
    getStudentBySearch,
    resetStudentPassword
} = require("../../../utils/request/manage/studentManage/studentManage")
import Toast from 'tdesign-miniprogram/toast/index';
import ActionSheet, {
    ActionSheetTheme
} from 'tdesign-miniprogram/action-sheet/index';
// pages/manage/studentManage/studentManage.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        departmentName: '',
        studentSearchForm: {
            departmentId: null,
            majorId: null,
            classId: null,
            name: '',
            pageSize: 10,
            pageNo: 1,
            total: 10
        },
        departmentPicker: false,
        majorPicker: false,
        classPicker: false,
        departmentTable: [],
        majorTable: [],
        classTable: [],
        studentTable: []
    },

    // 打开院系选择框
    onDepartmentPicker() {
        this.setData({
            departmentPicker: true
        })
        getDepartmentBySearch().then(res => {
            let modifiedData = res.data.data.tableData.map(item => {
                return {
                    label: item.departmentName,
                    value: item.departmentId
                };
            });
            // 使用 setData 方法更新数据
            this.setData({
                departmentTable: modifiedData
            });
        })
    },
    onMajorPicker() {
        this.setData({
            majorPicker: true
        })
        const form = {
            departmentId: this.data.studentSearchForm.departmentId,
            pageSize: -1,
            pageNo: -1
        }
        getMajorBySearch(form).then(res => {
            let modifiedData = res.data.data.tableData.map(item => {
                return {
                    label: item.majorName,
                    value: item.majorId
                }
            })
            this.setData({
                majorTable: modifiedData
            })
        })
    },
    onClassPicker() {
        this.setData({
            classPicker: true
        })
        const form = {
            departmentId: this.data.studentSearchForm.departmentId,
            majorId: this.data.studentSearchForm.majorId,
            pageSize: -1,
            pageNo: -1
        }
        getClassBySearch(form).then(res => {
            let modifiedData = res.data.data.tableData.map(item => {
                return {
                    label: item.className,
                    value: item.classId
                }
            })
            this.setData({
                classTable: modifiedData
            })
        })
    },
    // 选择院系
    onDepartmentChange(event) {
        this.setData({
            'studentSearchForm.departmentId': event.detail.value[0],
            'studentSearchForm.departmentName': event.detail.label[0]
        });
        this.getStudentList()
    },
    onMajorChange(event) {
        this.setData({
            'studentSearchForm.majorId': event.detail.value[0],
            'studentSearchForm.majorName': event.detail.label[0]
        })
        this.getStudentList()
    },
    onClassChange(event) {
        this.setData({
            'studentSearchForm.classId': event.detail.value[0],
            'studentSearchForm.className': event.detail.label[0]
        })
        this.getStudentList()
    },
    onDepartmentCancel() {
        this.setData({
            departmentPicker: false
        })
    },
    onMajorCancel() {
        this.setData({
            majorPicker: false
        })
    },
    onClasCancel() {
        this.setData({
            classPicker: false
        })
    },
    getStudentList() {
        this.setData({
            'studentSearchForm.pageNo': 1
        })
        getStudentBySearch(this.data.studentSearchForm).then(resp => [
            this.setData({
                studentTable: resp.data.data.tableData
            })
        ])
    },
    getMoreStudent() {
        this.setData({
            'studentSearchForm.pageNo': this.data.studentSearchForm.pageNo + 1
        })
        getStudentBySearch(this.data.studentSearchForm).then(resp => {
            this.setData({
                studentTable: this.data.studentTable.concat(resp.data.data.tableData)
            })
        })
    },
    handleUserNumberInput(e) {
        const value = e.detail.value
        this.setData({
            'studentSearchForm.userNumber': value
        })
    },
    handleNameInput(e) {
        const value = e.detail.value
        this.setData({
            'studentSearchForm.name': value
        })
    },
    handleStudentAction(event) {
        ActionSheet.show({
            theme: ActionSheetTheme.List,
            selector: '#t-action-students',
            context: this,
            align: 'left',
            description: '操作',
            items: [{
                    label: '重置密码',
                    icon: 'refresh',
                    suffixIcon: 'chevron-right',
                    action: 'handleRemove',
                    color: '#ed7b2f',
                    data: event.currentTarget.dataset.item.userNumber
                },
                {
                    label: '变更班级',
                    icon: 'arrow-left-right-1',
                    suffixIcon: 'chevron-right',
                    color: '#00a870',
                    action: 'handleSign',
                    data: event.currentTarget.dataset.item.userNumber
                }
            ],
        });
    },
    handleStudentSelected(e) {
        const data = e.detail.selected
        switch (e.detail.index) {
            case 0:
                this.handleResetPassword()
                break;
        }
    },
    handleResetPassword(userNumber) {
        const form = {
            userNumber: userNumber
        }
        const data = [form]
        resetStudentPassword(data).then(resp => {
            if(resp.data.code === 200){
                Toast({
                    context: this,
                    selector: '#t-toast',
                    message: '重置成功',
                    icon: 'check-circle',
                    direction: 'column',
                  });
            }
        })
    },
    handleUpdateClass(userNumber) {

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getStudentList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.getStudentList()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        this.getMoreStudent()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})